package fragmentson;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.wq.cycling.DemoApplication;
import com.wq.cycling.R;
import com.wq.cycling.ui.ChatActivity;

import java.util.ArrayList;
import java.util.List;

import activity.SonActivity;
import bean.EntityForSimple;
import fragmentson.backhandle.BackHandledFragment;
import util.adapter.ShopParticularsAdapter;
import util.cache.ImageLoaderForReal;
import util.myview.springview.DefaultFooter;
import util.myview.springview.DefaultHeader;
import util.myview.springview.SpringView;

import static android.R.attr.width;

/**
 * 维修店详情页
 * author：wangqi on 2017/4/26 17:48
 * email：773630555@qq.com
 */
public class ShopParticulars extends BackHandledFragment {

	private View rootView,headView;
	private ListView lv_shopparticulars;
	private ShopParticularsAdapter adapter;

	private SpringView springView;
	private ArrayList<EntityForSimple> mDatas = new ArrayList<>();

	private ImageLoaderForReal imageLoaderForReal;
	private List<View> mViews;
	private List<ImageView> pointViews;
	private ArrayList<String> murls =new ArrayList<>();
	String longitude = "116.407525";
	String latitude = "39.90403";
	private EntityForSimple mData = new EntityForSimple();
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		rootView = getLayoutInflater(savedInstanceState).inflate(R.layout.fragment_shopparticulars, null);
		mData = (EntityForSimple) getArguments().getSerializable("mData");
		if(!mData.getImg1().isEmpty()){
			murls.add(mData.getImg1());
		}
		if(!mData.getImg2().isEmpty()){
			murls.add(mData.getImg2());
		}
		if(!mData.getImg3().isEmpty()){
			murls.add(mData.getImg3());
		}
		//murls.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1496167590521&di=a784ce731ea30cbf50f2f389de69ae1c&imgtype=0&src=http%3A%2F%2Fimg.zcool.cn%2Fcommunity%2F05e5e1554af04100000115a8236351.jpg");
		//murls.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1496167590521&di=2908627d4d1ddfe8c33b54073a699f3b&imgtype=0&src=http%3A%2F%2Fimg.sj33.cn%2Fuploads%2Fallimg%2F201302%2F1-130201105055.jpg");
		//murls.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1496167590521&di=a784ce731ea30cbf50f2f389de69ae1c&imgtype=0&src=http%3A%2F%2Fimg.zcool.cn%2Fcommunity%2F05e5e1554af04100000115a8236351.jpg");
		initHeadView();
		initView();
		return rootView;
	}

	public void initHeadView(){
		imageLoaderForReal = new ImageLoaderForReal(getActivity());
		headView = LayoutInflater.from(getActivity()).inflate(R.layout.listview_shop_particulars_head_item,null);
		headView.findViewById(R.id.tv_comment).setOnClickListener(listener);
		String urls = "http://restapi.amap.com/v3/staticmap?location="+mData.getLon()+","+mData.getLat()+"&zoom=19&size="+width+"*180&markers=mid,,"+(1)+":"+longitude+","+latitude+"&key=8af1a80659f356662e0cea031a3264c4";
		imageLoaderForReal.DisplayImage(urls, ((ImageView) headView.findViewById(R.id.iv_map)));
		((ImageView) headView.findViewById(R.id.iv_map)).setOnClickListener(listener);
		headView.findViewById(R.id.ll_chat).setOnClickListener(listener);
		headView.findViewById(R.id.ll_call).setOnClickListener(listener);
		((TextView)headView.findViewById(R.id.tv_name)).setText(mData.getName());
		((TextView)headView.findViewById(R.id.tv_address)).setText(mData.getAddress());

		/*加载viewpager*/
		LinearLayout linear = (LinearLayout) headView.findViewById(R.id.linear);
		pointViews = new ArrayList<>();
		mViews = new ArrayList<>();
		for(int i=0;i<murls.size();i++){
			View view = getActivity().getLayoutInflater().inflate(R.layout.viewpager_shop_item, null);
			ImageView imageView = (ImageView) view.findViewById(R.id.imageview);
			imageLoaderForReal.DisplayImage(murls.get(i),imageView);
			mViews.add(view);
			ImageView v= new ImageView(getActivity());
			if(i==0){
				v.setId(i);
				v.setImageResource(R.mipmap.point_checked);
			}else{
				v.setId(i);
				v.setImageResource(R.mipmap.point_unchecked);
			}
			v.setPadding(10, 0, 10, 0);
			linear.addView(v);
			pointViews.add(v);
		}
		ViewPager mViewPager = (ViewPager) headView.findViewById(R.id.viewpager);
		mViewPager.setAdapter(new MyPagerAdapter());
		mViewPager.addOnPageChangeListener(new MyPagerOnPageChangeListener());
	}

	public void initView(){
		((TextView) rootView.findViewById(R.id.tv_title)).setText(R.string.shop_more);
		/*for(int i=0;i<20;i++){
			mDatas.add(new EntityForSimple());
		}*/
		lv_shopparticulars = ((ListView) rootView.findViewById(R.id.lv_shopparticulars));
		adapter = new ShopParticularsAdapter(getActivity(),mDatas);
		lv_shopparticulars.addHeaderView(headView);
		lv_shopparticulars.setAdapter(adapter);
		//initSpringView();
	}

	private class MyPagerAdapter extends PagerAdapter {
		@Override
		public int getCount() {
			return mViews.size();
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			return arg0 == (View) arg1;
		}


		@Override
		public Object instantiateItem(View v, int position) {
			((ViewPager) v).addView(mViews.get(position));
			return mViews.get(position);
		}

		@Override
		public void destroyItem(View v, int position, Object arg2) {
			((ViewPager) v).removeView(mViews.get(position));

		}
	}

	private class MyPagerOnPageChangeListener implements ViewPager.OnPageChangeListener {

		@Override
		public void onPageScrollStateChanged(int arg0) {
			System.out.println("arg0-1:" + arg0);

		}

		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2) {
			System.out.println("arg0-2:" + arg0);
		}

		@Override
		public void onPageSelected(int position) {
			//更新页码
			for(ImageView v:pointViews){
				v.setImageResource(R.mipmap.point_unchecked);
			}
			pointViews.get(position).setImageResource(R.mipmap.point_checked);
		}
	}


	/**
	 * 初始化springview组件
	 */
	private void  initSpringView(){
		springView = (SpringView) rootView.findViewById(R.id.springview);
		springView.setType(SpringView.Type.FOLLOW);
		springView.setListener(new SpringView.OnFreshListener() {
			@Override
			public void onRefresh() {
				new Handler().postDelayed(new Runnable() {
					@Override
					public void run() {
						springView.onFinishFreshAndLoad();
					}
				}, 1000);
			}

			@Override
			public void onLoadmore() {
				new Handler().postDelayed(new Runnable() {
					@Override
					public void run() {
						springView.onFinishFreshAndLoad();
					}
				}, 1000);
			}
		});
		springView.setHeader(new DefaultHeader(getActivity()));
		springView.setFooter(new DefaultFooter(getActivity()));
	}

	public View.OnClickListener listener = new View.OnClickListener() {
		@Override
		public void onClick(View v) {
			switch (v.getId()){
				case R.id.ll_call:
					DemoApplication.call(mData.getTelephone());
					break;
				case R.id.iv_map:
					((SonActivity) getActivity()).addFragment(ShopMap.newInstance(mData.getLat(),mData.getLon()));
					break;
				case R.id.ll_chat:
					startActivity(new Intent(getActivity(), ChatActivity.class).putExtra("userId", "yangkang"));
					break;
				case R.id.tv_comment:
					((SonActivity) getActivity()).addFragment(ShopComment.newInstance());
					break;
			}
		}
	};

	public static ShopParticulars newInstance(EntityForSimple mData) {
		ShopParticulars fragment = new ShopParticulars();
		Bundle bundle = new Bundle();
		bundle.putSerializable("mData",mData);
		fragment.setArguments(bundle);
		return fragment;
	}

	@Override
	public boolean onBackPressed() {
		return false;
	}
	
}
